package com.bt.lms.ui.page.sys.role;

import com.bt.lms.main.BotLmsApp;
import com.bt.lms.ui.page.sys.utils.MyButton;
import com.bt.lms.ui.page.sys.utils.MyHBox;
import com.bt.lms.ui.page.sys.utils.MyVBox;
import com.bt.lms.ui.page.sys.role.action.RAUReturnBtnAction;
import com.bt.lms.ui.vo.PageSkipInfo;
import com.bt.lms.ui.page.sys.vo.UserListInfo;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色添加用户界面
 */
@Deprecated
public class PageAddUser extends VBox {

    /**
     * 角色编码
     */
    private final String coding;

    /**
     * 界面跳转信息
     */
    private final PageSkipInfo pageSkipInfo;

    /**
     * 构建界面
     *
     * @param coding       角色编码
     * @param pageSkipInfo 界面跳转信息
     */
    public PageAddUser(String coding, PageSkipInfo pageSkipInfo) {

        //自己可能用的东西
        this.setId("PageList");
        this.coding = coding;
        this.pageSkipInfo = pageSkipInfo;

        //大小
        this.setPrefWidth(BotLmsApp.PER_WIGHT);
        this.setPrefHeight(BotLmsApp.HEIGHT);

        //布局
        Insets insets = new Insets(0);
        this.setPadding(insets);

        //组件
        //定位区域
        MyHBox locationHBox = new MyHBox();
        locationHBoxInit(locationHBox);
        ObservableList<Node> locationChildren = this.getChildren();
        locationChildren.add(locationHBox);

        //列表区域
        MyVBox listVBox = new MyVBox();
        listVBoxInit(listVBox);
        ObservableList<Node> listChildren = this.getChildren();
        listChildren.add(listVBox);

        //搜索区域
        MyHBox searchHBox = new MyHBox();
        searchHBoxInit(searchHBox);
        ObservableList<Node> searchChildren = this.getChildren();
        searchChildren.add(searchHBox);

        //操作区域
        MyHBox operateHBox = new MyHBox();
        operateHBoxInit(operateHBox);
        ObservableList<Node> operateChildren = this.getChildren();
        operateChildren.add(operateHBox);

    }

    /**
     * 操作区域初始化
     *
     * @param operateHBox 操作区域
     */
    private void operateHBoxInit(MyHBox operateHBox) {

        //布局
        Insets insets = new Insets(10);
        operateHBox.setPadding(insets);
        operateHBox.setSpacing(10);
        operateHBox.setAlignment(Pos.CENTER);

        //组件（返回按钮）
        //组件（确认按钮
        MyButton affirmBtn = new MyButton("确认");
        MyButton MyButton = new MyButton("返回");
        ObservableList<Node> children = operateHBox.getChildren();
        children.addAll(affirmBtn, MyButton);
        //事件
        RAUReturnBtnAction RAUReturnBtnAction = new RAUReturnBtnAction(pageSkipInfo);
        MyButton.setOnAction(RAUReturnBtnAction);

    }


    /**
     * 搜索区域初始化
     *
     * @param searchHBox 搜索区域
     */
    private void searchHBoxInit(HBox searchHBox) {

        //数据控制
        int width = 45;
        int sumStrip = 100;//总条数
        int present = 10;//当前页数
        String sumStripStr = "总记录数" + sumStrip + "条";
        String presentStr = "/" + present;

        //布局
        Insets insets = new Insets(10);
        searchHBox.setPadding(insets);
        searchHBox.setSpacing(10);
        searchHBox.setAlignment(Pos.CENTER_RIGHT);

        //组件
        Label firstLab = new Label("« 首页");
        searchHBox.getChildren().add(firstLab);
        Label lastLab = new Label("« 上一页");
        searchHBox.getChildren().add(lastLab);
        MyButton pageOne = new MyButton("1");
        searchHBox.getChildren().add(pageOne);
        MyButton pageTwo = new MyButton("2");
        searchHBox.getChildren().add(pageTwo);
        MyButton pageThree = new MyButton("3");
        searchHBox.getChildren().add(pageThree);
        Label nextLab = new Label("下一页 »");
        searchHBox.getChildren().add(nextLab);
        Label finallyLab = new Label("尾页 »");
        searchHBox.getChildren().add(finallyLab);
        Label sumNumberLab = new Label(sumStripStr);
        searchHBox.getChildren().add(sumNumberLab);
        TextField pageNumberTF = new TextField();
        pageNumberTF.setPrefWidth(width);
        searchHBox.getChildren().add(pageNumberTF);
        Label presentLab = new Label(presentStr);
        searchHBox.getChildren().add(presentLab);
        MyButton notarizeBtn = new MyButton("GO");
        searchHBox.getChildren().add(notarizeBtn);

    }

    /**
     * 列表区域初始化
     *
     * @param listVBox 列表区域
     */
    private void listVBoxInit(MyVBox listVBox) {

        //列表区域的提示初始化
        hintLabInit(listVBox);

        //列表
        //1.建立列表视图
        TableView<UserListInfo> tableView = new TableView<>();

        //2.列头
        //2.1 新建列头
        TableColumn<UserListInfo, HBox> operateTC = new TableColumn<>("操作");
        TableColumn<UserListInfo, String> idTC = new TableColumn<>("账号");
        TableColumn<UserListInfo, String> nameTC = new TableColumn<>("用户姓名");
        TableColumn<UserListInfo, String> roleTC = new TableColumn<>("角色");
        TableColumn<UserListInfo, String> creatorTC = new TableColumn<>("创建人");
        TableColumn<UserListInfo, LocalDate> creationTimeTC = new TableColumn<>("创建时间");
        TableColumn<UserListInfo, LocalDateTime> loginTimeTC = new TableColumn<>("最近登录时间");
        TableColumn<UserListInfo, String> stateTC = new TableColumn<>("状态");

        //2.2 列头添加到视图
        tableView.getColumns().addAll(operateTC, idTC, nameTC, roleTC, creatorTC, creationTimeTC, loginTimeTC, stateTC);

        //3.模拟数据
        //3.1 新建多个假数据
        List<UserListInfo> userListInfos = simulatedData();
        //3.2 格式转化(用户数据集合转换成Javafx组件集合)
        ObservableList<UserListInfo> users = FXCollections.observableArrayList(userListInfos);
        //3.3 数据添加到表格
        tableView.setItems(users);

        //4.映射（让每一列知道要展示的属性是哪一个）
        idTC.setCellValueFactory(new PropertyValueFactory<>("accountNumber"));
        nameTC.setCellValueFactory(new PropertyValueFactory<>("name"));
        roleTC.setCellValueFactory(new PropertyValueFactory<>("role"));
        creatorTC.setCellValueFactory(new PropertyValueFactory<>("creator"));
        creationTimeTC.setCellValueFactory(new PropertyValueFactory<>("creationTime"));
        loginTimeTC.setCellValueFactory(new PropertyValueFactory<>("loginTime"));
        stateTC.setCellValueFactory(new PropertyValueFactory<>("status"));
        operateTC.setCellValueFactory(new PropertyValueFactory<>("operate"));

        //试图添加
        ObservableList<Node> tableChildren = listVBox.getChildren();
        tableChildren.add(tableView);

    }


    /**
     * 列表区域的提示初始化
     *
     * @param listVBox 列表盒子
     */
    private void hintLabInit(MyVBox listVBox) {

        //组件(操作按钮)
        String hintStr = "添加" + coding + "角色用户";
        Label hintLab = new Label(hintStr);
        //按钮内部布局
        Insets insets = new Insets(10);
        hintLab.setPadding(insets);

        //提示列表（让操作按钮有间隔）
        HBox addUserBtnBox = new HBox();
        Insets addUserBtnBoxInsets = new Insets(10);
        addUserBtnBox.setPadding(addUserBtnBoxInsets);
        //盒子内添加标签
        addUserBtnBox.getChildren().add(hintLab);
        //添加到列表容器
        ObservableList<Node> addUserChildren = listVBox.getChildren();
        addUserChildren.add(addUserBtnBox);
    }

    /**
     * 定位区域初始化
     *
     * @param locationHBox 定位区域容器
     */
    private void locationHBoxInit(MyHBox locationHBox) {

        //控制数据
        int height = 42;

        //大小
        locationHBox.setPrefHeight(height);

        //布局
        Insets insets = new Insets(5);
        locationHBox.setPadding(insets);
        locationHBox.setSpacing(0);
        locationHBox.setAlignment(Pos.CENTER_LEFT);

        //组件
        Label siteLab = new Label("当前位置：");
        Label oneCatalogLab = new Label("🏠>系统管理");
        Label twoCatalogLab = new Label(">角色权限管理");
        Label fourCatalogLab = new Label(">添加角色用户");
        Label fiveCatalogLab = new Label(">" + coding + "角色");

        //添加组件
        locationHBox.getChildren().addAll(siteLab, oneCatalogLab, twoCatalogLab, fourCatalogLab,fiveCatalogLab);

    }

    /**
     * 列表界面模拟数据
     *
     * @return 模拟好的数据集合
     */
    private List<UserListInfo> simulatedData() {

        //数据总集合
        List<UserListInfo> userListInfos = new ArrayList<>();

        //模拟第一条数据
        oneSimulateData(userListInfos);

        //模拟第二条数据
        twoSimulateData(userListInfos);

        //模拟第三条数据
        threeSimulateData(userListInfos);

        //模拟第四条数据
        fourSimulateData(userListInfos);

        //返回全部数据
        return userListInfos;
    }

    /**
     * 第四条数据模拟
     *
     * @param userListInfos 总数据集合
     */
    private void fourSimulateData(List<UserListInfo> userListInfos) {

        //第四条
        //操作按钮盒子
        HBox operateHBox = new HBox();
        operateHBox.setPadding(new Insets(2));
        operateHBox.setSpacing(10);

        //操作按钮
        CheckBox checkBox = new CheckBox();
        operateHBox.getChildren().add(checkBox);

        //用户账号
        Label emailLab = new Label("luxiaoqi@163.com");
        //创建时间
        LocalDateTime creationTime1 = LocalDateTime.of(2023, 4, 27,13, 20);
        //最近登录时间
        LocalDateTime loginTime1 = LocalDateTime.of(2023, 4, 27, 13, 20);
        //用户状态
        Label stateLab = new Label("启用");

        //界面数据对象
        UserListInfo userListInfo = new UserListInfo( "陆小七", "系统管理员", creationTime1,
                loginTime1);
        userListInfos.add(userListInfo);

    }

    /**
     * 第三条数据模拟
     *
     * @param userListInfos 总数据集合
     */
    private void threeSimulateData(List<UserListInfo> userListInfos) {

        //第一条
        //操作按钮盒子
        HBox operateHBox = new HBox();
        operateHBox.setPadding(new Insets(2));
        operateHBox.setSpacing(10);

        //操作按钮
        CheckBox checkBox = new CheckBox();
        operateHBox.getChildren().add(checkBox);

        //创建时间
        LocalDateTime creationTime1 = LocalDateTime.of(2023, 4, 27,13, 20);
        //最近登录时间
        LocalDateTime loginTime1 = LocalDateTime.of(2023, 4, 27, 13, 20);
        //用户账号
        Label emailLab = new Label("zhonglou@163.com");
        //用户状态
        Label stateLab = new Label("启用");

        //界面数据对象
        UserListInfo userListInfo = new UserListInfo( "钟楼", "系统管理员",  creationTime1,
                loginTime1);
        userListInfos.add(userListInfo);

    }

    /**
     * 第二条数据模拟
     *
     * @param userListInfos 总数据集合
     */
    private void twoSimulateData(List<UserListInfo> userListInfos) {

        //第一条
        //操作按钮盒子
        HBox operateHBox = new HBox();
        operateHBox.setPadding(new Insets(2));
        operateHBox.setSpacing(10);

        //操作按钮
        CheckBox checkBox = new CheckBox();
        operateHBox.getChildren().add(checkBox);

        //创建时间
        LocalDateTime creationTime1 = LocalDateTime.of(2023, 4, 27, 13, 20);
        //最近登录时间
        LocalDateTime loginTime1 = LocalDateTime.of(2023, 4, 27, 13, 20);
        //用户账号
        Label emailLab = new Label("luxiaoq@163.com");
        //用户状态
        Label stateLab = new Label("启用");

        //界面数据对象
        UserListInfo userListInfo = new UserListInfo( "陆小七", "学习中心管理员，专业管理员，系统管理员", creationTime1, loginTime1);
        userListInfos.add(userListInfo);

    }

    /**
     * 第一条数据模拟
     *
     * @param userListInfos 总数据集合
     */
    private void oneSimulateData(List<UserListInfo> userListInfos) {

        //第一条
        //操作按钮盒子
        HBox operateHBox = new HBox();
        operateHBox.setPadding(new Insets(2));
        operateHBox.setSpacing(10);

        //操作按钮
        CheckBox checkBox = new CheckBox();
        operateHBox.getChildren().add(checkBox);

        //创建时间
        LocalDateTime creationTime1 = LocalDateTime.of(2023, 4, 27, 13, 20);
        //最近登录时间
        LocalDateTime loginTime1 = LocalDateTime.of(2023, 4, 27, 13, 20);
        //用户账号
        Label emailLab = new Label("luxiaoq@163.com");
        //用户状态
        Label stateLab = new Label("启用");

        //界面数据对象
        UserListInfo userListInfo = new UserListInfo( "陆小七", "学习中心管理员，系统管理员", creationTime1, loginTime1);
        userListInfos.add(userListInfo);

    }

}
